package com.swak.kryo;

import org.nustaq.serialization.FSTConfiguration;

import com.swak.exception.SerializeException;
import com.swak.serializer.Serializer;

public class FstSerializer implements Serializer {

	private FSTConfiguration fstConfiguration;

	public FstSerializer() {
		fstConfiguration = FSTConfiguration.getDefaultConfiguration();
		fstConfiguration.setClassLoader(Thread.currentThread().getContextClassLoader());
	}

	@Override
	public String name() {
		return "fst";
	}

	@Override
	public byte[] serialize(Object obj) throws SerializeException {
		return fstConfiguration.asByteArray(obj);
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T deserialize(byte[] bytes) throws SerializeException {
		return (T) fstConfiguration.asObject(bytes);
	}
}
